package org.exria.facevook.ctrlapp.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.exria.data.MapData;
import org.exria.data.RowSet;
import org.exria.db.query.SQLParam;
import org.exria.facevook.util.UtilString;
import org.exria.framework.dao.GeneralDAO;

public class FacevookController {
	public void chkmail(HttpServletRequest req, HttpServletResponse res) {
		String mail = req.getParameter("reqmail");

		GeneralDAO dao = new GeneralDAO();
		SQLParam param = new SQLParam();

		if (mail != null && mail.trim().length() != 0) {
			param.putWhereParam("mail", mail);
		}

		RowSet rs = (RowSet)dao.execute("/vookjoin", "chkmail", param);
		
		rs.setMidName("row");
		MapData root = new MapData("root");
		root.put("mailchk",rs);
		System.out.println();
		System.out.println(rs.toString());
		System.out.println();
		PrintWriter pw;

		try {
			pw = res.getWriter();
			pw.print(root);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public void vookjoin(HttpServletRequest req, HttpServletResponse res) {
		System.out.println("======================>vookjoin");
		String fname = req.getParameter("fname");
		String lname = req.getParameter("lname");
		String email = req.getParameter("email");
		String passwd = req.getParameter("passwd");
		String sex = req.getParameter("sex");
		String birthday = req.getParameter("birthday");
		

		GeneralDAO dao = new GeneralDAO();
		SQLParam param = new SQLParam();

		if (!UtilString.isNullData(email)) {
			param.putValueParam("fname", fname);
			param.putValueParam("lname", lname);
			param.putValueParam("email", email);
			param.putValueParam("passwd", passwd);
			param.putValueParam("sex", sex);
			param.putValueParam("birthday", birthday);
		} else {
			return;
		}
		

		int cnt = dao.executeUpdate("/vookjoin", "vookjoin", param);
		
		param.clear();
		MapData root = new MapData("root");
		root.put("resnum", cnt);
		PrintWriter pw;

		try {
			pw = res.getWriter();
			pw.print(root);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void getComboItem(HttpServletRequest req, HttpServletResponse res) {

		GeneralDAO dao = new GeneralDAO();
		SQLParam param = new SQLParam();

		RowSet rs = (RowSet) dao.execute("/Edu20120118", "combo", param);
		
		rs.setMidName("row");
		MapData root = new MapData("root");
		PrintWriter pw;

		System.out.println(root.toString());
		
		try {
			pw = res.getWriter();
			pw.print(root);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void login(HttpServletRequest req, HttpServletResponse res) {
		String mail = req.getParameter("mail");
		String passwd = req.getParameter("passwd");
		
	

		GeneralDAO dao = new GeneralDAO();
		SQLParam param = new SQLParam();

		if (!UtilString.isNullData(mail)&&!UtilString.isNullData(passwd)) {
			param.putWhereParam("mail", mail);
			param.putWhereParam("passwd", passwd);
		} else {
			return;
		}

		RowSet rs = (RowSet)dao.execute("/vooklogin", "login", param);
		param.clear();
		rs.setMidName("row");
		MapData root = new MapData("root");
		root.put("rescnt", rs);
		PrintWriter pw;

		if (rs.getRow(1).values().toString().equalsIgnoreCase("[1]")) {
			param.clear();
			param.putWhereParam("mail", mail);
			rs = (RowSet)dao.execute("/vooklogin", "loginT", param);
			req.getSession().setAttribute("rs", rs);
		}

		
		try {
			pw = res.getWriter();
			pw.print(root);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void sessionget(HttpServletRequest req, HttpServletResponse res) {

		RowSet rs = (RowSet) req.getSession().getAttribute("rs");
		MapData root;
		if(rs == null || rs.size() == 0){
			int a = 1;
			root = new MapData("root");
			root.put("rescnt", a);
			System.out.println(root.toString());
		}else{
			rs.setMidName("row");
			root = new MapData("root");
			root.put("sessionget", rs);
			System.out.println(root.toString());
		}
		PrintWriter pw;
		System.out.println(root.toString());
		
		try {
			pw = res.getWriter();
			pw.print(root);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
